from enum import Enum
from typing import List, Dict, Tuple, Optional, TypeVar, Generic, Any, Union
from dataclasses import dataclass, field
import copy

T = TypeVar('T')



class MachineType(Enum):
    '''机器类型 枚举'''
    PM = "PM"  # 加工机器
    TM = "TM"  # 机械臂
    LL = "LL"  # 真空锁
    AL = "AL"  # 校准设备


@dataclass
class ScheduleAssignment:
    """表示一个作业在一台机器上的调度分配"""
    job_id: int  # 作业ID
    machine_name: str  # 机器名称
    start_time: float  # 开始时间
    end_time: float  # 结束时间


@dataclass
class Machine:
    """一台机器"""

    # 构造函数（默认参数）
    def __init__(self, name: str = "", machine_type: MachineType = MachineType.PM):
        self.name: str = name  # 机器名称
        self.type: MachineType = machine_type  # 机器类型
        self.isFree: bool = True  # 机器是否空闲
        self.numProcessedItems: int = 0  # 已处理的工件数量
        self.startTime: List[int] = []  # 记录每次操作开始时间
        self.endTime: List[int] = []  # 记录每次操作结束时间
        self.availableTime: float = 0.0  # 记录最后一个的空闲时间
        self.availableTimes: List[Tuple[float, float]] = []  # 记录所有的空闲时间
        self.productTimes: List[Tuple[float, float]] = []  # 记录所有的生产时间


    def get_name(self) -> str:
        return self.name

    def set_name(self, name: str) -> None:
        self.name = name

    def get_num_processed_items(self) -> int:
        return self.numProcessedItems

    def set_num_processed_items(self, num: int) -> None:
        self.numProcessedItems = num

    def process_item(self) -> None:
        """处理一个新工件，增加计数"""
        self.numProcessedItems += 1

    def reset(self) -> None:
        """重置机器加工工件数量"""
        self.numProcessedItems = 0

    def is_free(self) -> bool:
        """检查机器是否空闲"""
        return self.isFree

    def set_option_free(self, free: bool) -> None:
        """设置机器空闲状态"""
        self.isFree = free


@dataclass
class MachinesResults:
    """表示整个调度方案的结果"""
    makespan: float = 0.0  # 最大完工时间
    schedule_results: List[ScheduleAssignment] = field(default_factory=list)  # 调度分配结果
    machines_map: Dict[str, Machine] = field(default_factory=dict)  # 机器字典


# 具体机器类型实现
class PM(Machine):
    """加工机器(Processing Machine)"""

    def __init__(self, name: str = ""):
        super().__init__(name=name, type=MachineType.PM)
        # PM特有的初始化逻辑


class TM(Machine):
    """机械臂(Transfer Machine)"""

    def __init__(self, name: str = ""):
        super().__init__(name=name, type=MachineType.TM)
        # TM特有的初始化逻辑


class LL(Machine):
    """真空锁(Load Lock)"""

    def __init__(self, name: str = ""):
        super().__init__(name=name, type=MachineType.LL)
        # LL特有的初始化逻辑


class AL(Machine):
    """校准设备(Alignment)"""

    def __init__(self, name: str = ""):
        super().__init__(name=name, type=MachineType.AL)
        # AL特有的初始化逻辑